package com.skycong.hello77.activity.common.callback;

import android.app.DownloadManager;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.skycong.hello77.activity.common.entiry.RSBase;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Kiki on 2018/5/28.
 */

public abstract class ModelCallback extends Callback<String> {
    protected static final String TAG = "ModelCallback";
    //    protected RefreshListFragmentBase refreshListFragmentBase;
    //请求地址类型 客户端负载均衡处理
    protected int urlType;
    protected String url = "";
    protected String paramsJson = "";

    public void setUrlType(int urlType) {
        this.urlType = urlType;
    }
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setParamsJson(String paramsJson) {
        this.paramsJson = paramsJson;
    }

    /**
     * 获取Model的类型
     */
    protected abstract Class<?> getModleClass();

    /**
     * 网络请求前调用的方法
     * @param request
     */
    public abstract void before(DownloadManager.Request request);
    /***
     * 网络请求后调用的方法
     */
    public abstract void after();

    /***
     * 网络不通调用的方法
     */
    public abstract void onNetworknotvalide();

    /***
     * 请求成功返回成功调用的方法
     *
     * @param t 返回的Modle实例
     */
    public abstract void onSuccess(RSBase t);

    /***
     * 请求成功，但是返回success=false的调用方法
     *
     * @param t LTBase实例（可查看失败原因）
     */
    public abstract void onSericeErr(RSBase t);

    /***
     * 请求异常时调用的方法
     *
     * @param e
     */
    public abstract void onSericeExp(Exception e);

    @Override
    public void onBefore(Request request) {
        super.onBefore(request);
       // before(request);
    }

    @Override
    public void onAfter() {
        super.onAfter();
        after();
    }

    @Override
    public void inProgress(float progress) {
        super.inProgress(progress);
        Log.i(TAG, "inProgress progress " + progress);
    }

    @Override
    public String parseNetworkResponse(Response response) throws Exception {
        return response.body().string();
    }

    @Override
    public void onError(Call call, Exception e) {
        Log.i(TAG, "url: " + url);
        Log.i(TAG, "params: " + paramsJson);
        Log.i(TAG, "onError call " + call.toString() + " e " + e.getMessage());
        onSericeExp(e);
        //连接超时则更换服务器地址
//        if (e instanceof java.net.SocketTimeoutException) {
//            if (urlType == UrlType.认证中心url.type) {
//            } else {
//                ApiUrlChangePresenter.getInstance().changeUrl();
//            }
//        }
    }

    @Override
    public void onResponse(String response) {
        Log.i(TAG, "url: " + url);
        Log.i(TAG, "params: " + paramsJson);
        Log.i(TAG, "onResponse " + response.toString());

        //DevelopModel.addLog(url, paramsJson, response.toString());

        if (TextUtils.isEmpty(response)) {
            onSericeErr(new RSBase(false, "return is null"));
            return;
        }

        RSBase base = null;
        try {
            base = (RSBase) new Gson().fromJson(response.toString(), getModleClass());
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        }

        if (base == null) {
            onSericeErr(new RSBase(false, "return is null"));
            return;
        }

        if (base.success) {
            onSuccess(base);
        } else {
//            if (ApiErrCode.token失效.errCode.equals(base.message)) {
//                TokenVerifyPresenter.getInstance().requestToken();
//            } else {
                onSericeErr(base);
            //}
        }
    }
}
